package com.wenhai.exercise;

import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;

public class HolidayUtil {

    /**
     * @param httpArg :参数
     * @return 返回结果
     */

    public static int request(String httpArg) {

        String httpUrl = "http://www.easybots.cn/api/holiday.php";

        BufferedReader reader = null;

        String result = null;

        StringBuffer sbf = new StringBuffer();

        httpUrl = httpUrl + "?d=" + httpArg;

        int d = 0;

        try {

            URL url = new URL(httpUrl);

            HttpURLConnection connection = (HttpURLConnection) url

                    .openConnection();

            connection.setRequestMethod("GET");

            connection.connect();

            InputStream is = connection.getInputStream();

            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));

            String strRead = null;

            while ((strRead = reader.readLine()) != null) {

                sbf.append(strRead);

                sbf.append("\r\n");

            }

            reader.close();

            result = sbf.toString();

            JSONObject ob = JSONObject.parseObject(result);

            if (ob != null) {

                d = Integer.parseInt(ob.getString("data"));

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

        return d;

    }


    public static void main(String[] args) {

        //判断今天是否是工作日 周末 还是节假日

        SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");

        String httpArg = "20190902";//f.format(new Date());

        System.out.println(httpArg);

        request(httpArg);

    }

}